﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    private static string strConn = ConfigurationManager.ConnectionStrings["LabConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session.Clear();
            Session.Abandon();
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string username = Request.Form["txtUsername"];
        string password = Request.Form["txtPassword"];

        string query = "sp_login '" + username+ "', '" + password + "'";

        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter(query, strConn))
        {
            da.Fill(dt);
        }

        if (dt.Rows.Count > 0)
        {
            int userId = int.Parse(dt.Rows[0]["ID"].ToString());
            int jenis = int.Parse(dt.Rows[0]["JENIS"].ToString());
            string nama = dt.Rows[0]["NAMA"].ToString();
            string strJenis = dt.Rows[0]["JENIS_USER"].ToString();

            int lab = 0;
            if (dt.Rows[0]["LAB"] != DBNull.Value)
            {
                lab = int.Parse(dt.Rows[0]["LAB"].ToString());
            }

            Session["idUser"] = userId;
            Session["idJenisUser"] = jenis;
            Session["namaUser"] = nama;
            Session["idLab"] = lab;
            Session["jenisUser"] = strJenis;

            Response.Redirect("/");
        }
    }
}